package com.cloudxingkong.dianyinke.Student.Ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.NewInformationAdapter;
import com.cloudxingkong.dianyinke.Student.bean.News;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻资讯Activity
 *
 * @ name Zxrui
 */

public class NewsInformation extends BaseActivity implements AdapterView.OnItemClickListener {
	private int position = 0;
	private PullToRefreshListView lv_pulltorefresh;
	private List<News> datas;
	private View headerView;
	private NewInformationAdapter adapter;
	private SharedPreferences sharedPreferences;
	private ImageView headImage;
	private TextView headText;
	private News news;
	private ListView refreshableView;

	protected void onCreateView (Bundle savedInstanceState) {
		setContentView (R.layout.activity_news_information);


		init ();

		loadData ();

		lv_pulltorefresh.setMode (PullToRefreshBase.Mode.BOTH);//支持上下刷新
		//上拉时显示的文字
		lv_pulltorefresh.setPullLabel ("上拉加载！", PullToRefreshListView.Mode.PULL_UP_TO_REFRESH);
		//提示刷新的文字
		lv_pulltorefresh.setReleaseLabel ("正在为您加载更多!", PullToRefreshListView.Mode.PULL_UP_TO_REFRESH);
		//正在刷新的文字
		lv_pulltorefresh.setRefreshingLabel ("释放刷新！", PullToRefreshListView.Mode.PULL_UP_TO_REFRESH);

		lv_pulltorefresh.setOnRefreshListener (new PullToRefreshBase.OnRefreshListener2<ListView> ()

											   {
												   @Override//下拉刷新
												   public void onPullDownToRefresh (PullToRefreshBase<ListView> refreshView) {
													   position = 0;
													   adapter.clear ();
													   loadData ();
												   }

												   @Override//上拉加载
												   public void onPullUpToRefresh (PullToRefreshBase<ListView> refreshView) {
													   position += 20;
													   adapter.clear ();
													   loadData ();
												   }
											   }

		);
	}

	private void loadData () {
		final String sessionID = CookieHelper.getSessionID (sharedPreferences);
		final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.NEWSINFORMATION;
		RequestParams params = new RequestParams (upLoadUrl);
		params.addHeader ("cookie", sessionID);
		params.addBodyParameter ("", "");
		x.http ().get (params, new Callback.CommonCallback<String> () {
			@Override
			public void onSuccess (String result) {
				Log.e ("test", "新闻资讯" + result);
				Message message = handler.obtainMessage ();
				message.what = 1;
				message.obj = result;
				handler.sendMessage (message);
			}

			@Override
			public void onError (Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled (CancelledException cex) {

			}

			@Override
			public void onFinished () {

			}
		});
	}

	private Handler handler = new Handler () {
		@Override
		public void handleMessage (Message msg) {
			super.handleMessage (msg);
			switch (msg.what) {
				case 1:
					String state = (String) msg.obj;
					if (state != null) {
						news = (new Gson ()).fromJson (state, News.class);
						String title = news.getHeadnews ().get (0).getTitle ();
						String titlepic = news.getHeadnews ().get (0).getTitlepic ();
						ImageLoader.getInstance ().displayImage (titlepic, headImage);
						headText.setText (title);
						datas.add (news);
						lv_pulltorefresh.setAdapter (adapter);
						adapter.notifyDataSetChanged ();
						lv_pulltorefresh.onRefreshComplete ();

					} else {
						Toast.makeText (getApplication (), "新闻正在赶路...", Toast.LENGTH_SHORT).show ();
					}
					break;
				case 2:
					loadData ();
					lv_pulltorefresh.onRefreshComplete ();
					break;
			}
		}
	};

	private void init () {
		sharedPreferences = NewsInformation.this.getSharedPreferences ("data_dyk", 0);
		lv_pulltorefresh = ((PullToRefreshListView) findViewById (R.id.lv_pulltorefresh));
		datas = new ArrayList<> ();
		adapter = new NewInformationAdapter (datas, this);
		try {
			headerView = LayoutInflater.from (this).inflate (R.layout.newsinformation_head, null);
			headerView.setOnClickListener (new View.OnClickListener () {
				@Override
				public void onClick (View v) {
					Intent intent = new Intent (NewsInformation.this, NewsInformationDetail.class);
					intent.putExtra ("title", news.getHeadnews ().get (0).getTitle () + "");
					intent.putExtra ("time", news.getHeadnews ().get (0).getDate () + "");
					intent.putExtra ("content", news.getHeadnews ().get (0).getContent () + "");
					intent.putExtra ("pic", news.getHeadnews ().get (0).getHeadpic () + "");
					intent.putExtra ("pic2", news.getHeadnews ().get (0).getTitlepic () + "");
					startActivity (intent);
				}
			});
			headImage = (ImageView) headerView.findViewById (R.id.news_headView);
			headText = (TextView) headerView.findViewById (R.id.news_headTitle);
			refreshableView = lv_pulltorefresh.getRefreshableView ();

			refreshableView.addHeaderView (headerView);

			refreshableView.setOnItemClickListener (this);
		} catch (Exception e) {
			Log.e ("test", "新闻资讯 " + e.getMessage ());
		}
	}

	//返回按钮
	public void back (View view) {
		finish ();
	}


	@Override
	public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent (NewsInformation.this, NewsInformationDetail.class);
		position -= refreshableView.getHeaderViewsCount ();
		intent.putExtra ("id", news.getNewslist ().getList ().get (position).getId ());
		intent.putExtra ("title", news.getNewslist ().getList ().get (position).getTitle () + "");
		intent.putExtra ("time", news.getNewslist ().getList ().get (position).getDate () + "");
		intent.putExtra ("content", news.getNewslist ().getList ().get (position).getContent () + "");
		intent.putExtra ("pic", news.getNewslist ().getList ().get (position).getHeadpic () + "");
		intent.putExtra ("pic2", news.getNewslist ().getList ().get (position).getTitlepic () + "");
		startActivity (intent);
	}
}